//
//  FZEmoticonToolBarView.swift
//  FZSina_Swift
//
//  Created by 李忠 on 7/30/16.
//  Copyright © 2016 FZLZ. All rights reserved.
//

import UIKit

enum FZEmoticonToolBarType: Int {
    /// 默认
    case Default = 0
    // Emoji
    case Emoji = 1
    case Lxh = 2
}

class FZEmoticonToolBarView: UIStackView {
    
    private var currentButton: UIButton?
    
    var clickButonClosure:((type: FZEmoticonToolBarType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        self.axis = .Horizontal
        self.distribution = .FillEqually
        addChildButton("默认", imgNamed: "left")
        addChildButton("emoji", imgNamed: "mid")
        addChildButton("浪小花", imgNamed: "right")
        //设置第一个按钮为默认选中状态
        setCurrentButton(subviews.first as! UIButton)
    }
    
    func selectEmonticonButton(type: FZEmoticonToolBarType){
        let btn = subviews[type.rawValue] as! UIButton
        setCurrentButton(btn)
    }
    //设置选中按钮
    private func setCurrentButton(btn: UIButton){
        if btn == currentButton {
            return
        }
        currentButton?.selected = false
        
        btn.selected = true
        
        currentButton = btn
    }
    @objc private func clickButton(sender: UIButton){
        
        let index = subviews.indexOf(sender)!
        let type = FZEmoticonToolBarType(rawValue: index)!
        setCurrentButton(sender)
        clickButonClosure?(type: type)
    }
    //MARK: 添加按钮
    func addChildButton(title: String, imgNamed: String) {
        
        let btn = UIButton()
        btn.addTarget(self, action: #selector(clickButton(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        
        btn.setTitle(title, forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Selected)
        
        btn.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imgNamed)_normal"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "compose_emotion_table_\(imgNamed)_selected"), forState: UIControlState.Selected)
        addArrangedSubview(btn)
        
    }
    
}
